package com.laxcen.smoke.config;

import com.laxcen.smoke.dao.MyLogMapper;
import com.laxcen.smoke.dto.req.*;
import com.laxcen.smoke.entity.SysLog;
import com.laxcen.smoke.util.DBUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;

/** 系统日志：切面处理类 */
@Aspect
@Component
public class SysLogAspect {


    private static final Logger log = LogManager.getLogger(SysLogAspect.class);

    /**操作数据库 */
    @Autowired
    private MyLogMapper myLogMapper;


    //定义切点 @Pointcut
    //在注解的位置切入代码
    @Pointcut("@annotation(com.laxcen.smoke.config.MyLog)")
    public void logPoinCut() {
    }


    //切面 配置通知
    @After("logPoinCut()")         //AfterReturning
    public void saveOperation(JoinPoint joinPoint) {
        log.info("---------------接口日志记录---------------");
        //用于保存日志
        SysLog adminLog = new SysLog();

        //从切面织入点处通过反射机制获取织入点处的方法
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        //获取切入点所在的方法
        Method method = signature.getMethod();

        //获取操作--方法上的Log的值
        MyLog myLog = method.getAnnotation(MyLog.class);
//        if (myLog != null) {
            //保存操作事件
            String operation = myLog.operation();
            adminLog.setOperation(operation);

            log.info("operation=" + operation );
//        }

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String requestURL = request.getRequestURL().toString();
        adminLog.setLoginip(requestURL);
        //设备编号
//        adminLog.setDeviceno(devicename);
        //类型  0App  1Fc  2Web
        adminLog.setType(2);
        // 客户端ip
        String ip = request.getRemoteAddr();
        adminLog.setLoginip(ip);
        // 操作人账号、姓名（需要提前将用户信息存到session）
//        UserMessageRes user = (UserMessageRes) request.getSession().getAttribute("userInfo");
//        HttpSession session = request.getSession(true);
//        Cookie cookie = CookiesUtil.getCookieByName(request,"username");
//        String username =  request.getHeader("Cookie");
//
//        Map<String, Cookie> cookies = ((WebApplicationContext) jp.getArgs()[0])
//                .getContainerRequest().getCookies();

//        if (user != null) {
//            String userName = myLogMapper.getUsernameByAccount(user.getAccount());
//            adminLog.setUsername(user.getUsername());
//        }
        log.info("url=" + requestURL, "ip=" + ip);

        //设置url
        adminLog.setOpurl(requestURL);
        //aop获取账号
//        if ("登录".equals(operation)){
//            //获取目标方法的参数信息
//            Object[] obj = joinPoint.getArgs();
//            for (Object argItem : obj) {
//                UserSignReq advid = (UserSignReq) argItem;
//                username = myLogMapper.getUsernameByAccount(advid.getAccount());
//            }
//        }
        //创建时间
        adminLog.setCreatetime(new Date());
        //调用service保存Operation实体类到数据库
        adminLog.setId(String.valueOf(DBUtils.nextId()));
        if ("删除广告图".equals(operation)){
            //获取目标方法的参数信息
            Object[] obj = joinPoint.getArgs();
            for (Object argItem : obj) {
                CommonOneIdReq advid = (CommonOneIdReq) argItem;
                String smartno = myLogMapper.getSmartNoByAd(advid.getId());
                String username = advid.getUsername();
                adminLog.setUsername(username);
                adminLog.setDeviceno(smartno);
                myLogMapper.insert(adminLog);
            }
        }
        if ("添加广告图".equals(operation)){
            //获取目标方法的参数信息
            Object[] obj = joinPoint.getArgs();
            for (Object argItem : obj) {
                AdvertisementInsertReq advid = (AdvertisementInsertReq) argItem;
                List<String> smartno = myLogMapper.getSmartNo();
                StringBuffer smartnolist = new StringBuffer();
                for (int i = 0; i < smartno.size();i++){
                    smartnolist.append(smartno.get(i));
                }
                adminLog.setDeviceno(smartnolist.toString());
                myLogMapper.insert(adminLog);
            }
        }
        if ("修改广告图".equals(operation)){
            //获取目标方法的参数信息
            Object[] obj = joinPoint.getArgs();
            for (Object argItem : obj) {
                AdvertisementUpdateReq advid = (AdvertisementUpdateReq) argItem;
                List<String> smartno = myLogMapper.getSmartNo();
                StringBuffer smartnolist = new StringBuffer();
                for (int i = 0; i < smartno.size();i++){
                    if (null==smartnolist||"".equals(smartnolist)){
                        smartnolist.append(smartno.get(i));

                    }else {
                        smartnolist.append(","+smartno.get(i));
                    }
                }
                adminLog.setDeviceno(smartnolist.toString());
                String username = advid.getUsername();
                adminLog.setUsername(username);
                adminLog.setDeviceno(smartnolist.toString());
                myLogMapper.insert(adminLog);
            }
        }
        if ("处理告警异常".equals(operation)){
            //获取目标方法的参数信息
            Object[] obj = joinPoint.getArgs();
            for (Object argItem : obj) {
                AbnormalUpdateReq advid = (AbnormalUpdateReq) argItem;
                String smartno = myLogMapper.getSmartByAbnor(advid.getId());
                String username = advid.getUsername();
                adminLog.setUsername(username);
                adminLog.setDeviceno(smartno);
                myLogMapper.insert(adminLog);
            }
        }
        if ("APP在线升级".equals(operation)){
            //获取目标方法的参数信息
            Object[] obj = joinPoint.getArgs();
            for (Object argItem : obj) {
                AppUpGradeOneReq advid = (AppUpGradeOneReq) argItem;
                String smartno = advid.getSmartno();
                String username = advid.getUsername();
                adminLog.setUsername(username);
                adminLog.setDeviceno(smartno);
                myLogMapper.insert(adminLog);
            }
        }
        if ("APP在线全部升级".equals(operation)){
            //获取目标方法的参数信息
            Object[] obj = joinPoint.getArgs();
            for (Object argItem : obj) {
                AppUpGradeOneReq advid = (AppUpGradeOneReq) argItem;
                String smartno = advid.getSmartno();
                String username = advid.getUsername();
                adminLog.setUsername(username);
                adminLog.setDeviceno(smartno);
                myLogMapper.insert(adminLog);
            }
        }
        if ("添加智能柜".equals(operation)){
            //获取目标方法的参数信息
            Object[] obj = joinPoint.getArgs();
            for (Object argItem : obj) {
                BoxInsertReq advid = (BoxInsertReq) argItem;
                String smartno = advid.getNo();
                String username = advid.getUsername();
                adminLog.setUsername(username);
                adminLog.setDeviceno(smartno);
                myLogMapper.insert(adminLog);
            }
        }
        if ("删除智能柜".equals(operation)){
            //获取目标方法的参数信息
            Object[] obj = joinPoint.getArgs();
            for (Object argItem : obj) {
                CommonOneIdReq advid = (CommonOneIdReq) argItem;
                String smartno = myLogMapper.getSmartBySmartid(advid.getId());
                String username = advid.getUsername();
                adminLog.setUsername(username);
                adminLog.setDeviceno(smartno);
                myLogMapper.insert(adminLog);
            }
        }
        if ("修改智能柜".equals(operation)){
            //获取目标方法的参数信息
            Object[] obj = joinPoint.getArgs();
            for (Object argItem : obj) {
                BoxUpdateBox advid = (BoxUpdateBox) argItem;
                String smartno = myLogMapper.getSmartBySmartid(advid.getId());
                String username = advid.getUsername();
                adminLog.setUsername(username);
                adminLog.setDeviceno(smartno);
                myLogMapper.insert(adminLog);
            }
        }
        if ("添加实体柜".equals(operation)){
            //获取目标方法的参数信息
            Object[] obj = joinPoint.getArgs();
            for (Object argItem : obj) {
                AddBoardReq advid = (AddBoardReq) argItem;
                String smartno = myLogMapper.getSmartBySmartid(advid.getSmartid());
                String username = advid.getUsername();
                adminLog.setUsername(username);
                adminLog.setDeviceno(smartno);
                myLogMapper.insert(adminLog);
            }
        }
        if ("删除实体柜".equals(operation)){
            //获取目标方法的参数信息
            Object[] obj = joinPoint.getArgs();
            for (Object argItem : obj) {
                CommonOneIdReq advid = (CommonOneIdReq) argItem;
                String smartno = myLogMapper.getSmartByBoardid(advid.getId());
                String username = advid.getUsername();
                adminLog.setUsername(username);
                adminLog.setDeviceno(smartno);
                myLogMapper.insert(adminLog);
            }
        }
        if ("修改实体柜".equals(operation)){
            //获取目标方法的参数信息
            Object[] obj = joinPoint.getArgs();
            for (Object argItem : obj) {
                UpdateBoardReq advid = (UpdateBoardReq) argItem;
                String smartno = myLogMapper.getSmartByBoardid(advid.getBoardid());
                String username = advid.getUsername();
                adminLog.setUsername(username);
                adminLog.setDeviceno(smartno);
                myLogMapper.insert(adminLog);
            }
        }
        if ("添加格子".equals(operation)){
            //获取目标方法的参数信息
            Object[] obj = joinPoint.getArgs();
            for (Object argItem : obj) {
                BoxInsertBoxReq advid = (BoxInsertBoxReq) argItem;
                String smartno = myLogMapper.getSmartByBoardid(advid.getId());
                String username = advid.getUsername();
                adminLog.setUsername(username);
                adminLog.setDeviceno(smartno);
                myLogMapper.insert(adminLog);
            }
        }
//        if ("删除格子".equals(operation)){
//            //获取目标方法的参数信息
//            Object[] obj = joinPoint.getArgs();
//            for (Object argItem : obj) {
//                CommonOneIdReq advid = (CommonOneIdReq) argItem;
//                String smartno = myLogMapper.getSmartByBoxid(advid.getId());
//                String username = advid.getUsername();
//                adminLog.setUsername(username);
//                adminLog.setDeviceno(smartno);
//                myLogMapper.insert(adminLog);
//            }
//        }
//        if ("修改格子状态".equals(operation)){
//            //获取目标方法的参数信息
//            Object[] obj = joinPoint.getArgs();
//            for (Object argItem : obj) {
//                UpdateBoxStatusReq advid = (UpdateBoxStatusReq) argItem;
//                String boxid = myLogMapper.getboxidByboxonlyno(advid.getBoxonlyno());
//                String smartno = myLogMapper.getSmartByBoxid(boxid);
//                adminLog.setDeviceno(smartno);
//                myLogMapper.insert(adminLog);
//            }
//        }
        if ("远程开门".equals(operation)){
            //获取目标方法的参数信息
            Object[] obj = joinPoint.getArgs();
            for (Object argItem : obj) {
                RemoteOpenDoorReq advid = (RemoteOpenDoorReq) argItem;
                adminLog.setUsername(advid.getUsername());
                adminLog.setDeviceno(advid.getSmartno());
                myLogMapper.insert(adminLog);
            }
        }
        if ("修改智能柜密码".equals(operation)){
            //获取目标方法的参数信息
            Object[] obj = joinPoint.getArgs();
            for (Object argItem : obj) {
                SetPasswordBySmartReq advid = (SetPasswordBySmartReq) argItem;
                adminLog.setUsername(advid.getUsername());
                adminLog.setDeviceno(advid.getSmartno());
                myLogMapper.insert(adminLog);
            }
        }
        if ("APP版本添加".equals(operation)){
            //获取目标方法的参数信息
            Object[] obj = joinPoint.getArgs();
            for (Object argItem : obj) {
                AddAppVersionReq advid = (AddAppVersionReq) argItem;
                adminLog.setUsername(advid.getUsername());
                myLogMapper.insert(adminLog);
            }
        }
//        if ("APP版本修改".equals(operation)){
//            //获取目标方法的参数信息
//            Object[] obj = joinPoint.getArgs();
//            for (Object argItem : obj) {
//                UpdateAppVersionReq advid = (UpdateAppVersionReq) argItem;
//                adminLog.setUsername(advid.getUsername());
//                myLogMapper.insert(adminLog);
//            }
//        }
        if ("APP版本删除".equals(operation)){
            //获取目标方法的参数信息
            Object[] obj = joinPoint.getArgs();
            for (Object argItem : obj) {
                CommonOneIdReq advid = (CommonOneIdReq) argItem;
                adminLog.setUsername(advid.getUsername());
                myLogMapper.insert(adminLog);
            }
        }
        if ("设置验证码次数时间".equals(operation)){
            //获取目标方法的参数信息
            Object[] obj = joinPoint.getArgs();
            for (Object argItem : obj) {
                VerifyReq advid = (VerifyReq) argItem;
                adminLog.setUsername(advid.getUsername());
                myLogMapper.insert(adminLog);
            }
        }

        if ("添加用户".equals(operation)){
            //获取目标方法的参数信息
            Object[] obj = joinPoint.getArgs();
            for (Object argItem : obj) {
                UserInsertReq advid = (UserInsertReq) argItem;
                adminLog.setUsername(advid.getUser());
                myLogMapper.insert(adminLog);
            }
        }
        if ("修改用户".equals(operation)){
            //获取目标方法的参数信息
            Object[] obj = joinPoint.getArgs();
            for (Object argItem : obj) {
                UserUpdateReq advid = (UserUpdateReq) argItem;
                adminLog.setUsername(advid.getUsername());
                myLogMapper.insert(adminLog);
            }
        }
        if ("删除用户".equals(operation)){
            //获取目标方法的参数信息
            Object[] obj = joinPoint.getArgs();
            for (Object argItem : obj) {
                CommonOneIdReq advid = (CommonOneIdReq) argItem;
                adminLog.setUsername(advid.getUsername());
                myLogMapper.insert(adminLog);
            }
        }
        if ("批量删除用户".equals(operation)){
            //获取目标方法的参数信息
            Object[] obj = joinPoint.getArgs();
            for (Object argItem : obj) {
                CommonOneIdReq advid = (CommonOneIdReq) argItem;
                adminLog.setUsername(advid.getUsername());
                myLogMapper.insert(adminLog);
            }
        }
        if ("分配角色".equals(operation)){
            //获取目标方法的参数信息
            Object[] obj = joinPoint.getArgs();
            for (Object argItem : obj) {
                DistributeUserReq advid = (DistributeUserReq) argItem;
                adminLog.setUsername(advid.getUsername());
                myLogMapper.insert(adminLog);
            }
        }
        if ("添加资源".equals(operation)){
            //获取目标方法的参数信息
            Object[] obj = joinPoint.getArgs();
            for (Object argItem : obj) {
                AddResourceReq advid = (AddResourceReq) argItem;
                adminLog.setUsername(advid.getUsername());
                myLogMapper.insert(adminLog);
            }
        }
        if ("修改资源".equals(operation)){
            //获取目标方法的参数信息
            Object[] obj = joinPoint.getArgs();
            for (Object argItem : obj) {
                UpdateResourceReq advid = (UpdateResourceReq) argItem;
                adminLog.setUsername(advid.getUsername());
                myLogMapper.insert(adminLog);
            }
        }
        if ("删除资源".equals(operation)){
            //获取目标方法的参数信息
            Object[] obj = joinPoint.getArgs();
            for (Object argItem : obj) {
                CommonOneLongIdReq advid = (CommonOneLongIdReq) argItem;
                adminLog.setUsername(advid.getUsername());
                myLogMapper.insert(adminLog);
            }
        }
        if ("批量删除资源".equals(operation)){
            //获取目标方法的参数信息
            Object[] obj = joinPoint.getArgs();
            for (Object argItem : obj) {
                CommonOneIdReq advid = (CommonOneIdReq) argItem;
                adminLog.setUsername(advid.getUsername());
                myLogMapper.insert(adminLog);
            }
        }
        if ("添加角色".equals(operation)){
            //获取目标方法的参数信息
            Object[] obj = joinPoint.getArgs();
            for (Object argItem : obj) {
                AddRoleReq advid = (AddRoleReq) argItem;
                adminLog.setUsername(advid.getUsername());
                myLogMapper.insert(adminLog);
            }
        }
        if ("修改角色".equals(operation)){
            //获取目标方法的参数信息
            Object[] obj = joinPoint.getArgs();
            for (Object argItem : obj) {
                UpdateRoleReq advid = (UpdateRoleReq) argItem;
                adminLog.setUsername(advid.getUsername());
                myLogMapper.insert(adminLog);
            }
        }
        if ("删除角色".equals(operation)){
            //获取目标方法的参数信息
            Object[] obj = joinPoint.getArgs();
            for (Object argItem : obj) {
                CommonOneLongIdReq advid = (CommonOneLongIdReq) argItem;
                adminLog.setUsername(advid.getUsername());
                myLogMapper.insert(adminLog);
            }
        }
        if ("批量删除角色".equals(operation)){
            //获取目标方法的参数信息
            Object[] obj = joinPoint.getArgs();
            for (Object argItem : obj) {
                CommonOneIdReq advid = (CommonOneIdReq) argItem;
                adminLog.setUsername(advid.getUsername());
                myLogMapper.insert(adminLog);
            }
        }
        if ("分配角色".equals(operation)){
            //获取目标方法的参数信息
            Object[] obj = joinPoint.getArgs();
            for (Object argItem : obj) {
                DistributeUserReq advid = (DistributeUserReq) argItem;
                adminLog.setUsername(advid.getUsername());
                myLogMapper.insert(adminLog);
            }
        }




//        myLogMapper.insert(adminLog);
    }
}
